package com.ruoyi.starbucks.service;

import com.ruoyi.common.utils.http.HttpUtils;
import lombok.extern.slf4j.Slf4j;

import java.text.MessageFormat;

@Slf4j
public class SrmService3 {
    public static String userName = "rdzzg880918";

    public static String password = "abc880918";

    public static String itemId = "21262";

    public static String token = "";

    public static void Login()
    {
        String url = MessageFormat.format("http://106.53.151.225:8001/api/sign/username={0}&password={1}",userName,password);
        String json = HttpUtils.sendGet(url,null);
        log.info("登录,json="+json);
        if (json.startsWith("1"))
        {
            token = json.split("\\|")[1];
            log.info("登录,token="+token);
        }
    }

    public static String GetPhoneNo() {
        if (token==null || token.isEmpty())
        {
            Login();
        }
        String url = MessageFormat.format("http://106.53.151.225:8001/api/yh_qh/id={0}&operator=0&Region=0&card=0&phone=&loop=1&token={1}",itemId,token);
        String json = HttpUtils.sendGet(url,null);
        if (json.startsWith("1"))
        {
            String data = json.split("\\|")[1];
            log.info("获取到的手机号:"+data);
            return data;
        }
        return "";
    }

    public static String GetMessage(String phoneNo) {
        if (token==null || token.isEmpty())
        {
            Login();
        }
        String url = MessageFormat.format("http://106.53.151.225:8001/api/yh_qm/id={0}&phone={1}&t={2}&token={3}",itemId,phoneNo,userName,token);
        String json = HttpUtils.sendGet(url,null);
        if (json.startsWith("1"))
        {
            String data = json.split("\\|")[1];
            log.info("获取到的短信:"+data);
            return data;
        }
        return  MessageFormat.format("ERROR:尚未收到短信，请确保您已经向手机号{0}发送过短信",phoneNo);
    }

    public static String AddToBlackList(String phoneNo){
        if (token==null || token.isEmpty())
        {
            Login();
        }

        String url =MessageFormat.format("http://106.53.151.225:8001/api/yh_lh/id={0}&phone={1}&token={2}",itemId,phoneNo,token);
        String json = HttpUtils.sendGet(url,null);
        if (json.startsWith("1"))
        {
            String data = json.split("\\|")[1];
            log.info(data);
            return data;
        }
        return "ERROR:号码拉黑失败,请再试一次";
    }

    public static void main(String[] args) {
        /**
        *登录
        */
//        Login();
        /**
         * 获取手机号码
         */
         GetPhoneNo();
        /**
         * 获取短信
         */
//        GetMessage(GetPhoneNo());
//        GetMessage("16533210620");

        /**
         * 加入黑名单
         */
//        AddToBlackList(GetPhoneNo());
    }
}
